<?php

namespace App\Http\Controllers\Supplier;
use App\Models\Areas;
use App\Models\Category;
use App\Models\ShopUser;
use App\Models\ShopUserLog;
use App\Services\AlipayService;
use App\Services\CommonService;
use Illuminate\Http\Request;

//店铺信息
class ShopInfoController extends BaseController
{
    //店铺基础信息
    public function shopBaseInfo()
    {
        $select = array('id', 'photo', 'member_id', 'shopname', 'shoptype', 'account', 'account_type', 'faptype', 'shopcity', 'contract_type', 'sign_num', 'people', 'contact_tel', 'mobile');
        $shopUser = ShopUser::select($select)->where(array('member_id' => $this->shopUser['member_id']))->first();
        $shopUser = $shopUser ? $shopUser->toArray() : array();
        if (empty($shopUser)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺不存在');
        }
        $category = Category::GetParentCateById($shopUser['shoptype']);
        $shopUser['category_name'] = $category ? $category['name'] : ''; //经营项目
        $shopUser['account_name'] = ShopUser::getAccountName($shopUser['account']); //账户类型
        $shopUser['account_type_name'] = ShopUser::isAccountType($shopUser['account_type']); //是否风控
        $shopUser['faptype_name'] = ShopUser::getFaptypeName($shopUser['faptype']); //发票类型
        $shopUser['contract_type_name'] = ShopUser::getContractTypeName($shopUser['contract_type']); //签署合同类型
        $shopUser['sign_num_name'] = ShopUser::getSignNumName($shopUser['sign_num']); //签署方式
        $shopcity = $shopUser['shopcity'];
        if (empty($shopcity)) {
            $shopcity_prov_id = '';
            $shopcity_city_id = '';
            $shopcity_area_id = '';
        } else {
            $shopcity = explode('-', $shopcity);
            $prov_data = !empty($shopcity[0]) ? Areas::GetAreasByName(array('name' => $shopcity[0])) : '';
            $city_data = !empty($shopcity[1]) ? Areas::GetAreasByName(array('name' => $shopcity[1])) : '';
            $area_data = !empty($shopcity[2]) ? Areas::GetAreasByName(array('name' => $shopcity[2], 'pid' => $city_data['id'])) : '';
            $shopcity_prov_id = !empty($prov_data['id']) ? $prov_data['id'] : '';
            $shopcity_city_id = !empty($city_data['id']) ? $city_data['id'] : '';
            $shopcity_area_id = !empty($area_data['id']) ? $area_data['id'] : '';
        }
        $shopUser['shopcity_prov_id'] = $shopcity_prov_id;
        $shopUser['shopcity_city_id'] = $shopcity_city_id;
        $shopUser['shopcity_area_id'] = $shopcity_area_id;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $shopUser);
    }

    //根据id编辑店铺基础信息
    public function editShopBaseInfoById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        if (array_key_exists('mobile', $input)) {
            if (!isTel($input['mobile'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效客服电话');
            }
        }
        if (array_key_exists('shopname', $input)) {
            $select = array('member_id', 'id', 'shopname');
        } elseif (array_key_exists('faptype', $input)) {
            $select = array('member_id', 'id', 'faptype');
        } elseif (array_key_exists('shopcity', $input)) {
            $select = array('member_id', 'id', 'shopcity');
        } elseif (array_key_exists('contract_type', $input)) {
            $select = array('member_id', 'id', 'contract_type');
        } elseif (array_key_exists('sign_num', $input)) {
            $select = array('member_id', 'id', 'sign_num');
        } elseif (array_key_exists('mobile', $input)) {
            $select = array('member_id', 'id', 'mobile');
        }
        $shopUser = ShopUser::select($select)->where(array('id' => $input['id']))->first();
        $shopUser = $shopUser ? $shopUser->toArray() : array();
        $member_id = $shopUser['member_id'];
        unset($shopUser['member_id']);
        $before_data = array_diff_assoc($shopUser, $input); //比较两个数组的键和值，并返回差集
        $after_data = array_diff_assoc($input, $shopUser);
        $res = ShopUser::editShopUserById($input['id'], $input);
        if (!$res) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '数据未发生变化');
        }
        //添加操作记录
        ShopUserLog::addLog($this->users['id'], $member_id, 2, '编辑供应商', $before_data, $after_data, '');
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '编辑成功');
    }

    //店铺主体信息
    public function shopMainInfo()
    {
        $select = array('id', 'mechanism', 'companyid', 'company_area', 'company', 'shopcard', 'doorphoto', 'seal_url', 'faname', 'idcard', 'faidcard', 'faidcard_back');
        $shopUser = ShopUser::select($select)->where(array('member_id' => $this->shopUser['member_id']))->first();
        $shopUser = $shopUser ? $shopUser->toArray() : array();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $shopUser);
    }

    //店铺二维码
    public function shopQrcode()
    {
        $shopUser = ShopUser::GetShopUser(array('member_id' => $this->shopUser['member_id']));
        $res = AlipayService::shopQrCode($this->shopUser['member_id'], $shopUser['shopname']);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $res);
    }


}
